#include "mainwindow.h"

#include <QApplication>

#include "common.h"

#include "listwidget.h"

#include "tempprocess.h"

#include "beheavior.h"

#include "zoomlabel.h"

#include "imgshow.h"

#include "keypresseventhandler.h"


using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setAcceptDrops(true);
    TempProcess *tmpProc = w.getTempProcess();
    //设置屏幕分割
    QSplitter *vertSplitter = new QSplitter(Qt::Vertical);
    QSplitter *horzLeftSplitter = new QSplitter(Qt::Horizontal,vertSplitter);
    QSplitter *horzRightSplitter = new QSplitter(Qt::Vertical,horzLeftSplitter);

    vertSplitter->setAcceptDrops(true);
    horzLeftSplitter->setAcceptDrops(true);

    //设置布局
    QWidget *central = new QWidget();
    QWidget *centralBottom = new QWidget();
    QWidget *centralLeft = new QWidget();
    QWidget *centralUPRight = new QWidget();
    QWidget *centralUPDown = new QWidget();

    QVBoxLayout* hLay = new QVBoxLayout();
    QHBoxLayout* vSubLay = new QHBoxLayout(vertSplitter);
    QHBoxLayout* vUpLeftSubLay = new QHBoxLayout(horzLeftSplitter);
    QHBoxLayout* vUpRightSubLay = new QHBoxLayout(horzRightSplitter);
    QHBoxLayout* vUpDownSubLay = new QHBoxLayout(horzRightSplitter);

    //下半部分
    QPushButton *borwserButton = new QPushButton(QString("添加文件"));
    QPushButton *delButton = new QPushButton(QString("删除文件"));
    QPushButton *modifyButton = new QPushButton(QString("修改模式"));
    QPushButton *delSectButton = new QPushButton(QString("删除选区"));
    QPushButton *okButton = new QPushButton(QString("完成修改"));
    QPushButton *scaleButton = new QPushButton(QString("放大图像"));
    QPushButton *restoreButton = new QPushButton(QString("还原图像"));
    QPushButton *exportButton = new QPushButton(QString("导出"));

    vSubLay->addWidget(borwserButton);
    vSubLay->addWidget(delButton);
    vSubLay->addWidget(modifyButton);
    vSubLay->addWidget(delSectButton);
    vSubLay->addWidget(okButton);
    vSubLay->addWidget(scaleButton);
    vSubLay->addWidget(restoreButton);
    vSubLay->addWidget(exportButton);
    vSubLay->setDirection(QBoxLayout::LeftToRight);

    centralBottom->setLayout(vSubLay);

    //上半部分
    QLabel *fileListTitle = new QLabel(QString("文件列表"),horzLeftSplitter);
    ListWidget *fileList = new ListWidget();
    ZoomLabel *scrollArea = new ZoomLabel();

    scrollArea->setStyleSheet("QLabel {border: 2px solid blue;} QLabel {background-color: white;}");

    fileList->setDragEnabled(true);
    fileList->setAcceptDrops(true);
    fileList->viewport()->setAcceptDrops(true);
    fileList->setDragDropMode(QAbstractItemView::InternalMove);

    //右上部分，修改温度值
    QLabel *colLabelTitle = new QLabel(QString("行"));
    QLabel *colLabelText= new QLabel(QString("0"));
    QLabel *rowLabelTitle = new QLabel(QString("列"));
    QLabel *rowLabelText= new QLabel(QString("0"));
    QLabel *tmpLabel = new QLabel(QString("温度值"));
    QLineEdit *tmpEdit = new QLineEdit();
    //验证器
    KeyPressEventHandler *keyHandler = new KeyPressEventHandler(&w);

    QPushButton *confirmTmpEdit = new QPushButton(QString("修改"));
    QPushButton *confirmStatus = new QPushButton(QString("状态"));
    confirmStatus->setText(w.getStatus().second);
    confirmStatus->setCheckable(true);

    QFont font = tmpEdit->font();
    font.setPointSize(14);

    //tmpEdit->setValidator(validator);
    tmpEdit->installEventFilter(keyHandler);
    tmpEdit->setFixedHeight(30);
    tmpEdit->setFont(font);

    //布局
    vUpLeftSubLay->addWidget(fileListTitle);
    vUpLeftSubLay->addWidget(fileList);
    vUpLeftSubLay->setDirection(QBoxLayout::TopToBottom);

    vUpRightSubLay->addWidget(colLabelTitle);
    vUpRightSubLay->addWidget(colLabelText);
    vUpRightSubLay->addWidget(rowLabelTitle);
    vUpRightSubLay->addWidget(rowLabelText);
    vUpRightSubLay->addWidget(tmpLabel);
    vUpRightSubLay->addWidget(tmpEdit);
    vUpRightSubLay->addWidget(confirmTmpEdit);
    vUpRightSubLay->addWidget(confirmStatus);
    vUpRightSubLay->setDirection(QBoxLayout::LeftToRight);
    vUpRightSubLay->setSpacing(15);

    vUpDownSubLay->addWidget(scrollArea);
    vUpDownSubLay->setSpacing(0);

    centralLeft->setLayout(vUpLeftSubLay);
    centralUPRight->setLayout(vUpRightSubLay);
    centralUPDown->setLayout(vUpDownSubLay);

    horzRightSplitter->addWidget(centralUPRight);
    //horzRightSplitter->addWidget(scrollArea);
    horzRightSplitter->addWidget(centralUPDown);

    horzLeftSplitter->addWidget(centralLeft);
    horzLeftSplitter->addWidget(horzRightSplitter);

    horzLeftSplitter->setAcceptDrops(true);
    horzRightSplitter->setAcceptDrops(true);

    horzLeftSplitter->setStretchFactor(0,1);
    horzLeftSplitter->setStretchFactor(1,3);

    horzRightSplitter->setStretchFactor(0,1);
    horzRightSplitter->setStretchFactor(1,100);


    hLay->addWidget(vertSplitter);
    hLay->addWidget(centralBottom);
    hLay->setMargin(0);

    central->setLayout(hLay);

    w.setCentralWidget(central);

    //回车修改温度
    QObject::connect(tmpEdit,&QLineEdit::returnPressed,[tmpEdit,scrollArea,tmpProc,&w](){
        changeTemp(scrollArea,tmpEdit,tmpProc,&w);
        w.changeMode(0);
        scrollArea->setMode(0);
        scrollArea->tempPt.clear();
        scrollArea->refresh();
    });
    //修改温度
    QObject::connect(confirmTmpEdit,&QPushButton::clicked,[tmpEdit,scrollArea,tmpProc,&w](){
            changeTemp(scrollArea,tmpEdit,tmpProc,&w);
            w.changeMode(0);
            scrollArea->setMode(0);
            scrollArea->tempPt.clear();
            scrollArea->refresh();
    });
    //旋转或移动
    QObject::connect(confirmStatus,&QPushButton::clicked,[confirmStatus,scrollArea,&w](){
        if(w.getMode() != 1)
        {
             changeStatus(confirmStatus,scrollArea,&w);
        }
        else
        {
            confirmStatus->setChecked(0);
        }
    });
    //添加按钮
    QObject::connect(borwserButton,&QPushButton::clicked,[fileList,&w](){
        addFile(fileList, &w);
    });
    //删除选区按钮
    QObject::connect(delSectButton,&QPushButton::clicked,[scrollArea,&w](){
           if(w.getMode() == 1)
           {
               scrollArea->tempPt.clear();
               scrollArea->refresh();
           }
    });
    //还原
    QObject::connect(restoreButton,&QPushButton::clicked,[confirmStatus,fileList,&w,scrollArea,tmpProc](){
            w.changeMode(0);
            scrollArea->setMode(0);
            scrollArea->setEditStatus(false);
            scrollArea->tempPt.clear();
            scrollArea->poly.clear();
            scrollArea->regionTmp->clear();
            w.setStatus(0);
            scrollArea->setStatus(0);
            confirmStatus->setText(w.getStatus().second);
            confirmStatus->setChecked(0);
            reloadImg(fileList,scrollArea,&w,tmpProc);
    });
    //删除按钮
    QObject::connect(delButton,&QPushButton::clicked,[fileList,scrollArea,tmpProc,&w](){
        deleteFile(fileList, scrollArea,tmpProc,&w);
    });
    //浏览图片
    QObject::connect(fileList,&QListWidget::itemDoubleClicked,[fileList,&w,scrollArea,tmpProc,confirmStatus]()
    {
        confirmStatus->setChecked(0);
        showTempImg(fileList,scrollArea,&w,tmpProc);
    });
    //点击显示温度
    QObject::connect(scrollArea,&ZoomLabel::clicked,[scrollArea,colLabelText,rowLabelText,tmpEdit,&w,tmpProc]()
    {

        scrollArea->setFocusPolicy(Qt::StrongFocus);
        scrollArea->setFocus();
        double t = getTempByPixel(scrollArea,tmpProc);
        colLabelText->setText(QString::number(scrollArea->getMousePos().x()));
        rowLabelText->setText(QString::number(scrollArea->getMousePos().y()));
        tmpEdit->setText(QString::fromStdString(std::to_string(t)));
    });
    //修改模式按钮
    QObject::connect(modifyButton,&QPushButton::clicked,[confirmStatus,scrollArea,&w](){
        if(scrollArea->poly.empty())
        {
            w.changeMode(1);
            scrollArea->setMode(1);
            scrollArea->tempPt.clear();
            scrollArea->poly.clear();
            scrollArea->regionTmp->clear();
            w.setStatus(0);
            scrollArea->setStatus(0);
            confirmStatus->setText(w.getStatus().second);
             confirmStatus->setChecked(0);
        }
        else
        {
            QMessageBox::information(&w,"完成","无法修改");
        }
    });

    //完成修改按钮
    QObject::connect(okButton,&QPushButton::clicked,[scrollArea,tmpProc,&w](){
        w.changeMode(0);
        scrollArea->setMode(0);
        scrollArea->setPicOffset(QPoint(0,0));
        if(!scrollArea->tempPt.empty())
        {   std::map<QPoint*,QString> tempPoint;
            if(scrollArea->getRotateFactor() != 0)
            {
                QPoint rotateCenter = scrollArea->getCenterPos() + scrollArea->getPicOffset();
                double rAng = 360 - scrollArea->getRotateFactor();
                QMatrix transform;
                transform = transform.translate(rotateCenter.x(),rotateCenter.y()).rotate(rAng).translate(-(rotateCenter.x()),-(rotateCenter.y()));

                for(auto t:scrollArea->tempPt)
                {
                    QPoint* pt = new QPoint(((*(t.first))* scrollArea->getScaleFactor()) + scrollArea->getPicOffset());
                    *pt = transform.map(*pt);
                    tempPoint.insert(make_pair(pt,t.second));
                }
            }
            else
            {
                for(auto t:scrollArea->tempPt)
                {
                    QPoint* pt = new QPoint(((*(t.first))* scrollArea->getScaleFactor()) + scrollArea->getPicOffset());
                    tempPoint.insert(make_pair(pt,t.second));
                }
            }
            scrollArea->tempPt.clear();
            scrollArea->tempPt = tempPoint;
            std::vector<QPoint*> pts = tmpProc->getEnvelope(scrollArea->tempPt);
            for(auto k = pts.begin();k!=pts.end();k++)
            {
                scrollArea->poly.append(*(*k));
            }
            //auto ptfinal = pts.begin();
            //scrollArea->poly.append(*(*ptfinal));
            //scrollArea->poly.clear();
            //scrollArea->regionTmp->clear();
        }
        else
        {
            QMessageBox::information(&w,"完成","无法修改");
        }
        saveStatus(scrollArea,tmpProc,&w);
        scrollArea->refresh();
        scrollArea->setScaleFactor(1.0);


    });

    QObject::connect(scaleButton,&QPushButton::clicked,[scrollArea,tmpProc,&w](){
            QImage cuimg= scaledImg(scrollArea,tmpProc,&w);
            if(cuimg.isNull())
            {
                QMessageBox::warning(&w,"完成","无法查看图像");
                return;
            }
            cuimg = cuimg.scaled(cuimg.size() * 2);
            imgshow *ig = new imgshow(cuimg,&w);
            ig->exec();
    });

    QObject::connect(exportButton,&QPushButton::clicked,[scrollArea,tmpProc,&w](){
        if(w.env->getEnvironmentLists()->size() > 0)
        {
             exportRegions(scrollArea,tmpProc,&w);
        }
        else
        {
            QMessageBox::information(&w,"完成","无法导出");
        }
    });

    w.show();
    return a.exec();
}
